﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SPCFMedicineInformer.Members;
using Vega.USiteBuilder;
using umbraco.cms.businesslogic.member;

namespace SPCFMedicineInformer.Masterpages
{
    public partial class Admin_UserListings : System.Web.UI.MasterPage
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
                BindDropdown();
            }

        }
        void BindGrid()
        {
            var memberList = Roles.GetUsersInRole("Administrator")
                        .Select(user => MemberHelper.GetMemberFromLoginName(user) as WebUser);
            rptMemberListing.DataSource = memberList;
            rptMemberListing.DataBind();
        }
        void Delete(int id)
        {
            if (id > 0)
            {
                Member m = new Member(id);
                if (m != null)
                {
                    m.delete();   
                }
                BindGrid();
            }
        }
        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case "delete":
                        Delete(intTemp);
                        break;
                }
            }
        }
        protected void btnAddNewSlide_Click(object sender, EventArgs e)
        {
            Response.Redirect("/admin/users/admin-users/admin-user-details/");
        }
        void BindDropdown()
        {
            ddlTitles.Items.Add(new ListItem("Id", "Id"));
            ddlTitles.Items.Add(new ListItem("First Name", "First Name"));
            ddlTitles.Items.Add(new ListItem("Last Name", "Last Name"));
            ddlTitles.Items.Add(new ListItem("Email", "Email"));
        }
        protected void btnFilter_Click(object sender, EventArgs e)
        {
            IEnumerable<WebUser> list = Roles.GetUsersInRole("Administrator")
                    .Select(user=> MemberHelper.GetMemberFromLoginName(user) as WebUser)
                    .Where(a=>a.Fistname.ToLower().Contains(txtSearchFirstName.Text) && a.Lastname.ToLower().Contains(txtSearchLastName.Text)
                        && a.Email.ToLower().Contains(txtSearchEmail.Text));

            if (rdoSort.Text == "Asc")
            {
                switch (ddlTitles.SelectedValue)
                {
                    case "Id":
                        list = list.OrderBy(a => a.Id);
                        break;
                    case "First Name":
                        list = list.OrderBy(a => a.Fistname);
                        break;
                    case "Last Name":
                        list = list.OrderBy(a => a.Lastname);
                        break;
                    case "Email":
                        list = list.OrderBy(a => a.Email);
                        break;
                }
            }
            else
            {
                switch (ddlTitles.SelectedValue)
                {
                    case "Id":
                        list = list.OrderByDescending(a => a.Id);
                        break;
                    case "First Name":
                        list = list.OrderByDescending(a => a.Fistname);
                        break;
                    case "Last Name":
                        list = list.OrderByDescending(a => a.Lastname);
                        break;
                    case "Email":
                        list = list.OrderByDescending(a => a.Email);
                        break;
                }
            }



            rptMemberListing.DataSource = list;
            rptMemberListing.DataBind();

        }
    }
}